<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="物业名称" prop="pmid">
        <el-select v-model="queryParams.pmid" placeholder="请选择物业名称" @change="clearCarshomeList"
                   clearable>
          <el-option
            v-for="item in this.propertyList"
            :key="item.pmid"
            :label="item.propertyName"
            :value="item.pmid">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="车场名称" prop="chid">
        <el-select v-model="queryParams.chid" placeholder="请选择车场名称" @visible-change="getListCars" clearable>
          <el-option
            v-for="item in this.carshomeList"
            :key="item.chid"
            :label="item.chname"
            :value="item.chid">
          </el-option>
        </el-select>
      </el-form-item>




      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="equipmentList">

      <el-table-column label="车场设备id" align="center" prop="eqid"/>
      <el-table-column label="物业名称" align="center" prop="propertyManagement.propertyName"/>
      <el-table-column label="车场名称" align="center" prop="carsHome.chname"/>
      <el-table-column label="设备名称" align="center" prop="carsHomeLane.lanename"/>
      <el-table-column label="设备IP" align="center" prop="eqip"/>


      <el-table-column label="类型" align="center" prop="carsHomeLane.lanetype" :formatter="showLanetype"/>

      <el-table-column label="状态(上线下线）" align="center" prop="status" :formatter="showStatus"/>


      <!--      <el-table-column label="开闸关闸" align="center" prop="operate" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
        <el-button type="text" @click="handOpen(scope.row)">开闸</el-button>
        <el-button type="text" @click="handClose(scope.row)">关闸</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import {
  listEquipment,
  getEquipment,
  delEquipment,
  addEquipment,
  updateEquipment
} from "@/api/modules/sys-cars-home/equipment/equipment";
import {listHome} from "@/api/modules/sys-cars-home/home";
import {listProperty} from "@/api/modules/sys-property/property";

export default {
  name: "Equipment",
  data() {
    return {
      // 遮罩层
      loading: true,

      // 车场管理表格数据
      carshomeList: [],
      carshomeList2: [],

      // 物业数据
      propertyList: [],

      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车场设备表格数据
      equipmentList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        status: null,
        deleted: null,
        chlid: null,
        eqname: null,
        eqip: null,
        operate: null,

        pmid: null,
        propertyName: null,

        chid: null,
        chname: null
      },


      // 查询参数
      queryParams2: {
        pmid: null,
      },


      // 表单参数
      form: {},

    };
  },
  created() {
    this.getList();
    this.getListCars();
    this.getListProperty()
  },
  methods: {

    showLanetype(row, column, cellValue, index,) {
      if (cellValue) {
        return "入口"
      } else return "出口"
    },

    showStatus(row, column, cellValue, index,) {

      if (cellValue) {
        return "离线"
      } else return "在线"
    },
    /** 查询车场管理列表 */
    getListCars() {
      this.queryParams2.pmid = this.queryParams.pmid
      listHome(this.queryParams2).then(response => {
        this.carshomeList = response.rows;
      });
    },

    /** 查询物业列表 */
    getListProperty() {
      this.loading = true;
      listProperty(null).then(response => {
        this.propertyList = response.rows;
        this.loading = false;
      });
    },
    //清除车场
    clearCarshomeList() {
      this.queryParams2.pmid = this.queryParams.pmid
      this.queryParams.chid = null
      this.getListCars()
    },


    /** 查询车场设备列表 */
    getList() {
      this.loading = true;
      listEquipment(this.queryParams).then(response => {
        this.equipmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 表单重置
    reset() {
      this.form = {
        eqid: null,
        code: null,
        status: null,
        deleted: null,
        createTime: null,
        updateTime: null,
        chlid: null,
        eqname: null,
        eqip: null,
        operate: null
      };
      this.resetForm("form");
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handOpen(row) {
      this.queryParams=row
      if(this.queryParams.operate==1){
        this.queryParams.operate=0
        updateEquipment(this.queryParams).then(res=>{
          this.$message({
            message: '开闸成功',
            type: 'success'
          });
        })
      }else if(this.queryParams.operate==0){
        this.$message({
          message: '已开闸',
          type: 'success'
        });
      }



    },
    handClose(row) {
      this.queryParams=row
      if(this.queryParams.operate==0){
        this.queryParams.operate=1
        updateEquipment(this.queryParams).then(res=>{
          this.$message({
            message: '关闸成功',
            type: 'success'
          });
        })
      }else if(this.queryParams.operate==1){
        this.$message({
          message: '已关闸',
          type: 'success'
        });
      }
    }


  }
};
</script>
